package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.LoginBean;
import com.by.bean.RoleConstant;
import com.by.bean.User;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.service.UserService;
import com.by.token.TokenTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户管理")
@RequestMapping("/api")
public class  UserController extends BaseController {
    @Autowired
    UserService userService;

    @Autowired
    TokenTemplate tokenTemplate;
    @ApiImplicitParam(name = "account", value = "账号", required = true, dataTypeClass = String.class, paramType = "query")
    @ApiOperation( "检查账号是否存在")
    @GetMapping("/user/check")
    public ResultBean check(@RequestParam(required = true) String account) {
        int rs = userService.checkUser(account);
        String tmp = rs == 0? "no" : "yes";
        return success("账号是否存在", tmp);
    }

    @ApiOperation("用户登录")
    @PostMapping("/user/login")
    protected ResultBean login(@Validated @RequestBody LoginBean loginBean , BindingResult bindingResult) {
        V.validate(bindingResult);
        //根据用户名查询
        User user = userService.queryByAccount(loginBean.getAccount());
        if(user == null){
            return fail("账号不存在！");
        }
        //密码验证
        //先加密用户输入的密码
        String pwd = DigestUtils.md5DigestAsHex(loginBean.getPassword().getBytes());
        if(!pwd.equals(user.getPassword())){
            return fail("账号或密码错误！");
        }
        //判断账号是否被禁用
        if(user.getStatus() == 2){
            return fail("账号已被禁用，请联系管理员！");
        }
        //将用户信息转换为map
        Map map = BeanUtil.beanToMap(user);
        //将密码移除
        map.remove("password");
        map.put("username", user.getAccount());
        map.remove("account");
        String role = "";
        //处理角色
        switch (user.getType()){
            case 1:
                if ("admin".equals(user.getAccount())){
                    role = RoleConstant.ADMIN;
                }else {
                    role = RoleConstant.SALE;
                }
                break;
            case 2:
                role = RoleConstant.CUSTOMER;
                break;
            case 3:
                role = RoleConstant.SUPPLIER;
                break;
            case 4:
                role = RoleConstant.FORWARDER;
                break;
        }
        List<String> roleList = new ArrayList<>();
        roleList.add(role);
        map.put("roles", roleList);
        //生成token
        String token = tokenTemplate.generateToken(map);
        map.put("accessToken", token);


        return super.success("登录成功", map);
    }
}
